local items = { "①与你手牌数相同的角色数", "②你手牌数的上次变化量" }
local des = "转换技，你造成或受到伤害后，可以将手牌数调整为X（X为%s%s）。若因此弃牌，你可以防止此技能转换。"

Fk:loadTranslationTable{
  ["plum__xiejie"] = "谐节",
  [":plum__xiejie"] = "转换技，你造成或受到伤害后，可以将手牌数调整为X（X为①与你手牌数相同的角色数②你手牌数的上次变化量）。若因此弃牌，你可以防止此技能转换。",
  [":plum__xiejie_yang"] = string.format(des, "<font color= '#E0DB2F'>" .. items[1] .. "</font>", items[2]),
  [":plum__xiejie_yin"] = string.format(des, items[1], "<font color= '#E0DB2F'>" .. items[2] .. "</font>"),

  ["@plum__xiejie"] = "谐节",
  ["#plum__xiejie-ivk"] = "谐节：可以将手牌数调整为%arg",
  ["#plum__xiejie-notSwitch"] = "谐节：可以防止此技能转换",
}

local T = require "packages.tuntian_studio.utility"

local xiejie = fk.CreateSkill{
  name = "plum__xiejie",
  tags = {Skill.Switch}
}

--- damage
xiejie:addEffect(fk.Damage, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(xiejie.name) and player == target
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local toNum
    if player:getSwitchSkillState(xiejie.name, false) == fk.SwitchYang then
      toNum = #table.filter(room.alive_players, function (p) return p:getHandcardNum() == player:getHandcardNum() end)
    else
      toNum = player:getMark("@plum__xiejie")
    end
    if room:askToSkillInvoke(player, {skill_name = xiejie.name, prompt = "#plum__xiejie-ivk:::" .. toNum}) then
      event:setCostData(self, {toNum = toNum, notSwitch = (player:getHandcardNum() > toNum)})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local toNum = event:getCostData(self).toNum
    player:broadcastSkillInvoke(xiejie.name)
    if event:getCostData(self).notSwitch and room:askToSkillInvoke(player, {skill_name = xiejie.name, prompt = "#plum__xiejie-notSwitch"}) then
      room:setPlayerMark(player, MarkEnum.SwithSkillPreName .. xiejie.name, player:getSwitchSkillState(xiejie.name, true))
    else
      room:notifySkillInvoked(player, xiejie.name, "switch")
    end
    if player.dead then return end
    local ret = T.adjustHandcardNumTo(player, toNum, xiejie.name)
    if player.dead then return end
    room:setPlayerMark(player, "@plum__xiejie", ret[1])
  end
})

--- damaged
xiejie:addEffect(fk.Damaged, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    local room = player.room
    if player:hasSkill(xiejie.name) and player == target then
      local toNum
      if player:getSwitchSkillState(xiejie.name, false) == fk.SwitchYang then
        toNum = #table.filter(room.alive_players, function (p) return p:getHandcardNum() == player:getHandcardNum() end)
      else
        toNum = player:getMark("@plum__xiejie")
      end
      return toNum ~= player:getHandcardNum()
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local toNum
    if player:getSwitchSkillState(xiejie.name, false) == fk.SwitchYang then
      toNum = #table.filter(room.alive_players, function (p) return p:getHandcardNum() == player:getHandcardNum() end)
    else
      toNum = player:getMark("@plum__xiejie")
    end
    if room:askToSkillInvoke(player, {skill_name = xiejie.name, prompt = "#plum__xiejie-ivk:::" .. toNum}) then
      event:setCostData(self, {toNum = toNum, notSwitch = (player:getHandcardNum() > toNum)})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local toNum = event:getCostData(self).toNum
    player:broadcastSkillInvoke(xiejie.name)
    if event:getCostData(self).notSwitch and room:askToSkillInvoke(player, {skill_name = xiejie.name, prompt = "#plum__xiejie-notSwitch"}) then
      room:setPlayerMark(player, MarkEnum.SwithSkillPreName .. xiejie.name, player:getSwitchSkillState(xiejie.name, true))
    else
      room:notifySkillInvoked(player, xiejie.name, "switch")
    end
    if player.dead then return end
    local ret = T.adjustHandcardNumTo(player, toNum, xiejie.name)
    if player.dead then return end
    room:setPlayerMark(player, "@plum__xiejie", ret[1])
  end
})

return xiejie